async function getWeather() {
    const apiKey = 'SP2KHG2dkAwTC-lDN'; // 请替换为你自己的心知天气 API密钥
    const city = document.getElementById('city').value;
    const weatherDiv = document.getElementById('weather');

    if (city === '') {
        weatherDiv.innerHTML = '请输入城市名';
        return;
    }

    const url = `https://api.seniverse.com/v3/weather/now.json?key=${apiKey}&location=${city}&language=zh-Hans&unit=c`;

    try {
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();
        displayWeather(data);
    } catch (error) {
        weatherDiv.innerHTML = `Error: ${error.message}`;
    }
}

function displayWeather(data) {
    const weatherDiv = document.getElementById('weather');
    const { location, now } = data.results[0];
    const temperature = now.temperature;
    const weatherDesc = now.text;
    const weatherInfo = `
        <h2>${location.name}</h2>
        <p>温度: ${temperature}°C</p>
        <p>天气: ${weatherDesc}</p>
    `;
    weatherDiv.innerHTML = weatherInfo;
}